#include "Token.h"
using namespace std;
map<std::string, std::string> Token::tokenHash;

std::string Token::generateRandomString(int length)
{
    const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    std::random_device rd;
    std::mt19937 generator(rd());
    std::uniform_int_distribution<int> distribution(0, characters.size() - 1);

    std::string randomString;
    for (int i = 0; i < length; ++i)
    {
        randomString += characters[distribution(generator)];
    }

    return randomString;
}

Token::Token(int tokenlength) {

    do
    {
        token= generateRandomString(tokenlength);
    }while(tokenHash.find(token)!=tokenHash.end());
    tokenHash.insert(std::pair<std::string,std::string>(token,token));
}

std::string Token::findToken(std::string token) {
    return tokenHash.find(token)->first;
}

bool Token::removeToken(std::string token) {
    if(tokenHash.find(token)==tokenHash.end())
        return false;
    else
        return true;
}

std::string Token::getToken(){
    return token;
}
